//package com.vca.common.utils;
//
//
//import com.vca.common.exception.VcaException;
//import com.vca.common.vo.EmailVo;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import javax.mail.Authenticator;
//import javax.mail.Session;
//import javax.mail.Transport;
//import javax.mail.internet.InternetAddress;
//import javax.mail.internet.MimeMessage;
//import java.util.Date;
//import java.util.Properties;
//
///**
// * @Description:邮件发送工具类
// * @author:chenbing
// * @date 2022/12/5 15:54
// */
//public class SendEmailUtils {
//
//    public static final Logger logger = LoggerFactory.getLogger(SendEmailUtils.class);
//    private final static String MyEmail = "3091115941@qq.com";//开启授权码的邮箱
////    private final static String AuthorizationCode = "XZMZYXINPCYDFYLQ";//授权码
//    private final static String AuthorizationCode = "yyvenjbqukfzdfjf";//授权码
////    private final static String SMTPEmail = "smtp.yeah.net";// 网易163邮箱的 SMTP 服务器地址
//    private final static String SMTPEmail = "smtp.qq.com";// 网易163邮箱的 SMTP 服务器地址
//    private static String harvestEmail = "hunter_jackson@yeah.net";// 收件人邮箱
//
//    /**
//     * @Description:测试邮件发送
//     * @author:chenbing
//     * @date 2022/12/5 16:03
//     */
//    public static void main(){
//        EmailVo emailVo = new EmailVo();
//        emailVo.setHarvestEmail(harvestEmail);
//        emailVo.setSmtpEmail(SMTPEmail);
//        emailVo.setContent("你注册的账户现在已生效，请重新登录！");
//        emailVo.setMyEmail(MyEmail);
//        emailVo.setAuthorizationCode(AuthorizationCode);
//        emailVo.setHarvestName("Jacks");
//        emailVo.setMyName("L’ÉCOLE上海");
//        emailVo.setMessageSubject("注册账户已生效");
//        sendEmailProxy(emailVo);
//    }
//
//    public static void main(String[] args){
//        EmailVo emailVo = new EmailVo();
//        emailVo.setHarvestEmail(harvestEmail);
//        emailVo.setSmtpEmail(SMTPEmail);
//        emailVo.setContent("你注册的账户现在已生效，请重新登录！");
//        emailVo.setMyEmail(MyEmail);
//        emailVo.setAuthorizationCode(AuthorizationCode);
//        emailVo.setHarvestName("Jacks");
//        emailVo.setMyName("L’ÉCOLE上海");
//        emailVo.setMessageSubject("注册账户已生效");
//        sendEmailProxy(emailVo);
//    }
//
//    /**
//     * @Description:发送邮件
//     * @author:chenbing
//     * @date 2022/12/15 18:03
//     */
//    public static void sendEmail(EmailVo emailVo) {
//        try {
//            //创建连接邮件服务器的参数配置
//            Properties props = new Properties();// 参数配置
//            props.setProperty("mail.smtp.host", emailVo.getSmtpEmail());// 发件人的邮箱的 SMTP 服务器地址
//            props.setProperty("mail.smtp.auth", "true");// 需要请求认证
//            props.setProperty("mail.transport.protocol", "smtp");
//            //根据配置创建会话对象和邮件服务器交互
//            Session session = Session.getInstance(props);
//            session.setDebug(true);// 设置为debug模式, 可以查看详细的发送日志
//            //创建邮件
//            MimeMessage message = createEmail(session, emailVo.getMyEmail(), emailVo.getHarvestEmail(),
//                    emailVo.getContent(), emailVo.getMyName(), emailVo.getHarvestName(), emailVo.getMessageSubject());
//            //使用Session获取邮件传输对象
//            Transport transport = session.getTransport();
//            //使用邮箱账号和密码连接邮件服务器
//            transport.connect(emailVo.getMyEmail(), emailVo.getAuthorizationCode());
//            //发送邮件
//            transport.sendMessage(message, message.getAllRecipients());
//            //关闭连接
//            transport.close();
//        } catch (Exception e) {
//            throw new VcaException("发送邮件失败，"+e.getMessage());
//        }
//    }
//
//
//    public static void sendEmailProxy(EmailVo emailVo) {
//        try {
//            Properties props = System.getProperties();
//            String proxyHost = "zproxy-sh.cn.ali.rccad.net";
//            String proxyPort = "80";
//            props.setProperty("socksProxyHost", proxyHost);
//            props.setProperty("socksProxyPort", proxyPort);
//            //创建连接邮件服务器的参数配置
//            props.setProperty("mail.transport.protocol", "smtp");
//            props.setProperty("proxySet", "true");
//            props.setProperty("mail.smtp.host", emailVo.getSmtpEmail());// 发件人的邮箱的 SMTP 服务器地址
//            props.setProperty("mail.smtp.auth", "true");// 需要请求认证
//            props.setProperty("mail.debug", "true");
//            final String username = emailVo.getMyEmail();
//            final String password = emailVo.getAuthorizationCode();
//            //使用验证
//            //根据配置创建会话对象和邮件服务器交互
//            Session session = Session.getDefaultInstance(props,
//                    new Authenticator() {
//                        protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
//                            return new javax.mail.PasswordAuthentication(username,
//                                    password);
//                        }
//                    });
//            session.setDebug(true);
//            MimeMessage message = new MimeMessage(session);
//            //发件人
//            message.setFrom(new InternetAddress(emailVo.getMyEmail(),emailVo.getMyName(), "UTF-8"));
//            //收件人
//            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(emailVo.getHarvestEmail(), emailVo.getHarvestName(), "UTF-8"));
//            //邮件主题
//            message.setSubject(emailVo.getMessageSubject());
//            //邮件正文
//            message.setText(emailVo.getContent());
//            //设置发件时间
//            message.setSentDate(new Date());
//            Transport.send(message);
//            System.out.println("邮件发送！");
//        } catch (Exception e) {
//            throw new VcaException("发送邮件失败，"+e.getMessage());
//        }
//    }
//
//    /**
//     * @Description:创建邮件
//     * @author:chenbing
//     * @date 2022/12/15 18:14
//     */
//    public static MimeMessage createEmail(Session session, String sendMail, String receiveMail, String content, String myName, String harvestName, String messageSubject) {
//        try {
//            //创建一封邮件
//            MimeMessage message = new MimeMessage(session);
//            //发件人
//            message.setFrom(new InternetAddress(sendMail, myName, "UTF-8"));
//            //收件人
//            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, harvestName, "UTF-8"));
//            //邮件主题
//            message.setSubject(messageSubject, "UTF-8");
//            //邮件正文
//            message.setContent(content, "text/html;charset=UTF-8");
//            //设置发件时间
//            message.setSentDate(new Date());
//            //保存设置
//            message.saveChanges();
//            return message;
//        } catch (Exception e) {
//            throw new VcaException("创建邮件失败，" + e.getMessage());
//        }
//    }
//
//}
